#pragma once
#include "Util.h"
namespace xncc {
namespace msg {

    typedef uint64_t shm_queue_index_t;
    class SHMQueue
    {
      public:
        static SHMQueue*         createProducerQueue(shm_queue_index_t size, void* mem);
        static SHMQueue*         createConsumerQueue(void* mem);
        static shm_queue_index_t GetMemorySize(shm_queue_index_t size);  // 需要申请的真正的内存大小

        static void destroyQueue(SHMQueue* queue);

      public:
        inline shm_queue_index_t capacity() { return MASK + 1; }
        char*                    getWriteBuffer(shm_queue_index_t len);
        void                     finishWrite();
        char*                    getReadBuffer(shm_queue_index_t len);
        void                     finishRead();

      private:
        inline shm_queue_index_t unused() { return (MASK + 1) - (in - out); }
        SHMQueue(shm_queue_index_t size, void* mem);
        SHMQueue(void* mem);
        ~SHMQueue();

      private:
        shm_queue_index_t          MASK;
        volatile shm_queue_index_t out;
        shm_queue_index_t          currReadLen;
        volatile shm_queue_index_t in;
        shm_queue_index_t          currWriteLen;
        volatile shm_queue_index_t currentFinal;
    };

}  // namespace msg
}  // namespace xncc